package com.ITArmy.service.impl;

import com.ITArmy.beans.Focus;
import com.ITArmy.beans.Message;
import com.ITArmy.beans.Shows.UserFocus;
import com.ITArmy.beans.User;
import com.ITArmy.common.ServerResponse;
import com.ITArmy.dao.FocusMapper;
import com.ITArmy.dao.MessageMapper;
import com.ITArmy.dao.UserMapper;
import com.ITArmy.service.FocusService;
import com.ITArmy.util.CheckMessage;
import com.ITArmy.util.GetUserSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class FocusServiceImpl implements FocusService {
    @Autowired
    FocusMapper focusMapper;
    @Autowired
    UserMapper userMapper;
    @Autowired
    MessageMapper messageMapper;
    GetUserSession getUserSession = new GetUserSession();

    //添加关注(增)

    @Override
    public ServerResponse addFocus(HttpServletRequest request, long authorId) {
        User user = getUserSession.getUser(request);
        //先判断一下是否关注过，如果关注过那么
        if (user.getuId() == authorId) {
            return ServerResponse.createBySuccessMessage("您不能关注自己...");
        }

        if (focusMapper.checkIsFocus(authorId, user.getuId()) != null) {
            System.out.println("关注过...");
            return ServerResponse.createByErrorMesage("您已经关注过该作者");

        }
        Focus focus = new Focus();
        focus.setbUId(authorId);
        focus.setfUId(user.getuId());
        focus.setfTime(new Date());
        int resultCount = focusMapper.insertSelective(focus);
        //将关注消息储存到消息表中
        Message message = new Message();
        //关注产生的时间
        message.setTime(new Date());
        //关注者id
        message.setuId(getUserSession.getUser(request).getuId());
        //被关注者id
        message.setuCid(authorId);
        //关注id
        message.setfId(focusMapper.checkIsFocus(authorId, user.getuId()).getfId());
        //将消息储存到数据库中
        message.setStatus("1");
        messageMapper.insert(message);
        CheckMessage check = new CheckMessage();
        return check.getMessage(resultCount, "关注成功", "关注失败");
    }

    //取消关注（删）
    @Override
    public ServerResponse cancelFocus(HttpServletRequest request, long authorId) {
        User user = getUserSession.getUser(request);
        int resultCount = focusMapper.cancelFocus(user.getuId(), authorId);
        CheckMessage check = new CheckMessage();
        return check.getMessage(resultCount, "取消关注成功", "取消关注失败");
    }

    //遍历用户关注的其他人的信息
    @Override
    public ServerResponse findAllFocuses(HttpServletRequest request) {
        User user = getUserSession.getUser(request);
        List<Focus> focusList = focusMapper.selectFocusUsers(user.getuId());
        List<UserFocus> userFocusList = new ArrayList<>();
        System.out.println("集合的大大小为:" + focusList.size());
        if (focusList.size() > 0) {
            for (int i = focusList.size() - 1; i >= 0; i--) {
                User user1 = userMapper.selectByPrimaryKey(focusList.get(i).getfUId());
                if (user1 != null) {
                    userFocusList.add(new UserFocus(focusList.get(i), user1));
                    System.out.println("关注的信息" + focusList.get(i));
                }
            }
            return ServerResponse.createBySuccess("获取成功...", userFocusList);
        }
        return ServerResponse.createByErrorMesage("空空如也...");
    }

    //遍历用户的粉丝
    @Override
    public ServerResponse findFuns(HttpServletRequest request) {
        User user = getUserSession.getUser(request);
        List<Focus> focusList = focusMapper.selectFuns(user.getuId());
        List<User> fans = new ArrayList<User>();
        if (focusList.size() > 0) {

            for (int i = 0; i < focusList.size(); i++) {
                fans.add(userMapper.selectByPrimaryKey(focusList.get(i).getfUId()));
            }
            return ServerResponse.createBySuccess("获取成功...", fans);
        }
        return ServerResponse.createByErrorMesage("空空如也...");
    }
}
